<?php

namespace App\Http\Controllers\Admin\Erm;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;

/**
 * 预警设备监控
 */
class RiskDeviceController extends BaseController
{
    /**
     * @api {get} /erm/risk-device/list 预警设备监控列表
     * @apiVersion 1.5.0
     * @apiName 预警设备监控列表
     * @apiGroup 预警设备监控
    */
    public function list(Request $request)
    {
        $param = $request->only('user_code', 'risk_type', 'ip', 'page', 'page_size');
        $v = app('validator')->make($param, [
            'user_code' => 'string',
            'risk_type' => 'integer',
            'ip' => 'string',
            'page' => 'integer',
            'page_size' => 'integer',
        ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }
        $page = $request->input('page', 1);
        $perPage = $request->input('page_size', 10);

        $param['page'] = $param['page'] ?? 1;
        $param['page_size'] = $param['page_size'] ?? 10;

        $list = app('repo_erm_risk_device')->getRiskDevice($param, $page, $perPage);

        return Response::sendData($list);
    }

    /**
     * @api {get} /erm/risk-device/risk-type 预警设备监控列表
     * @apiVersion 1.5.0
     * @apiName 预警设备监控列表
     * @apiGroup 预警设备监控
    */
    public function getRiskType()
    {
        $list = app('repo_erm_risk_device')->getRiskType();

        return Response::sendData($list);
    }

    /**
    * @api {get} /erm/risk-device/white/list 预警设备白名单列表
    * @apiVersion 1.5.0
    * @apiName 预警设备白名单列表
    * @apiGroup 预警设备监控
    */
    public function getWhiteList(Request $request)
    {
        $param = $request->only('user_code', 'risk_type', 'device_id', 'page', 'page_size', 'start_add_time', 'end_add_time', 'operator_id');
        $v = app('validator')->make($param, [
            'page' => 'integer',
            'page_size' => 'integer',
            'user_code' => 'string',
            'risk_type' => 'integer',
            'device_id' => 'string',
            'start_add_time' => 'date',
            'end_add_time' => 'date',
            'operator_id' => 'integer',
        ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }
        $page = $request->input('page', 1);
        $perPage = $request->input('page_size', 10);

        $param['page'] = $param['page'] ?? 1;
        $param['page_size'] = $param['page_size'] ?? 10;

        $list = app('repo_erm_risk_device')->getWhiteList($param, $page, $perPage);

        return Response::sendData($list);
    }

    /**
    * @api {post} /erm/risk-device/white/list 添加到设备白名单列表
    * @apiVersion 1.5.0
    * @apiName 添加到设备白名单列表
    * @apiGroup 预警设备监控
    */
    public function addWhiteList(Request $request)
    {
        $param = $request->only('id');
        $v = app('validator')->make($param, [
            'id' => 'integer',
        ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }

        $param['operator_id'] = $request->admin_id ?? '';

        app('repo_erm_risk_device')->addWhiteList($param);

        return Response::sendData(true);
    }

    /**
    * @api {post} /erm/risk-device/white/remove 移除设备白名单
    * @apiVersion 1.5.0
    * @apiName 移除设备白名单
    * @apiGroup 预警设备监控
    */
    public function removeWhiteList(Request $request)
    {
        $param = $request->only('id');
        $v = app('validator')->make($param, [
            'id' => 'integer',
        ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }

        app('repo_erm_risk_device')->removeWhiteList($param['id']);

        return Response::sendData(true);
    }
}
